// -*- coding: utf-8 -*- 
/**
 * Project: LanQiao
 * Creator: yanking
 * Create time: 2022-01-18 19:46
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package TBinaryAdd;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.IntSupplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;


public class TBinaryAdd {
    // 实现32的二进制表示输出
    @Test
    public void TBinary() {
        int b = 0b00000;
        String[] supple = {"", "0", "00", "000", "0000",};
        int len = 0;
        for (int i = 0; i <= 31; i++) {
            String binaryNum = Integer.toBinaryString(b++);
            int sup = 5 - binaryNum.length();
            if (sup >= 0) {
                System.out.println(supple[sup] + binaryNum);
            }
        }
    }

    @Test
    public void TBinary02() {
        // 使用DecimalFormat方法格式化输出表示
        int b = 0b00000;
        DecimalFormat df = new DecimalFormat("00000");
        for (int i = 0; i < 32; i++) {
            System.out.printf("%s\n", df.format(Integer.valueOf(Integer.toBinaryString(b++))));
        }
    }






    @Test
    public void shout() {
        System.out.printf("%1$s %2$tB %2$te, %2$tY", "Due date:", new Date());
    }

    @Test
    public void shout01() {
        List<int[]> res = IntStream.rangeClosed(1, 100).boxed().flatMap(a -> IntStream.rangeClosed(a, 100).filter(
                b -> Math.sqrt(a * a + b * b) % 1 == 0).mapToObj(
                b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)})).collect(Collectors.toList());
        for (int[] ints : res) {
            for (int i : ints) {
                System.out.print(i + "\t");
            }
            System.out.println();
        }
    }

    @Test
    public void shout03() {
        // 避免流数据处理中计算了两次sqrt
        IntStream.rangeClosed(1, 100).boxed().flatMap(a -> IntStream.rangeClosed(a, 100).mapToObj(
                b -> new double[]{a, b, Math.sqrt(a * a + b * b)}).filter(t -> t[2] % 1 == 0));

    }


    @Test
    public void shout02() throws IOException {
        // USE NIO API NO-BLOCKED I/O API  TO GET THE  DATA OF A FILE
        String txtPath = "F:\\Java_idea\\LanQiao\\src\\TBinaryAdd\\data.txt";
        long count = Files.lines(Paths.get(txtPath), Charset.defaultCharset()).flatMap(
                lines -> Arrays.stream(lines.split(" "))).distinct().count();

    }

    @Test
    public void shout04() {
        System.out.println("Hello!");
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run() {
                System.out.println("Here is the run method in Privileged Action!");
                return null;
            }
        });
    }

    @Test
    public void shout05() {
        ClassLoader SCL = ClassLoader.getSystemClassLoader();
        System.out.println(SCL); // sun.misc.Launcher$AppClassLoader@18b4aac2
        System.out.println(SCL.getParent()); // sun.misc.Launcher$ExtClassLoader@a09ee92
        System.out.println(SCL.getParent().getParent()); // null
        try {
            Class aClass = Class.forName("java.text.DecimalFormat");
            Class bClass = Class.forName("com.extClass.stu");
            System.out.println(aClass.getClassLoader()); // null
            System.out.println(bClass.getClassLoader()); // sun.misc.Launcher$AppClassLoader@18b4aac2
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void shout06() {
        // 实现无限流以及无限流的截取
        Stream.iterate(0, n -> n + 2).limit(2).forEach(System.out::println);
    }

    @Test
    public void shout07() {
        Stream.generate(Math::random).limit(5).forEach(System.out::println);
    }

    @Test
    public void shout08() {
        Integer a = 1;
        Integer b = 1;
        System.out.println(a + b);
        Integer c = a + b;
        int d = a + b;
        System.out.println(c.getClass());
        System.out.println(d);
    }


    @Test
    public void shout09() {
        IntStream one = IntStream.generate(() -> 1).limit(1);
        IntStream two = IntStream.generate(() -> 2).limit(1);
        IntStream third = IntStream.generate(new IntSupplier() {
            @Override
            public int getAsInt() {
                return 3;
            }
        }).limit(1);
        one.forEach(System.out::println);
        two.forEach(System.out::println);
        third.forEach(System.out::println);
    }

    @Test
    public void shout10() {
        // 状态修改和状态保持
        IntSupplier FIB = new IntSupplier() {
            int preValues = 0;
            int curValues = 1;
            int oldValues = 0;

            @Override
            public int getAsInt() {
                oldValues = preValues;
                preValues = curValues;
                curValues = oldValues + curValues;
                return curValues;
            }
        };
        IntStream.generate(FIB).limit(20).forEach(a -> System.out.print(a + "\t"));
    }

    @Test
    public void shout11() {
        System.out.println(Stream.of(1, 2, 3, 4, 5).reduce(0, Integer::sum));
        System.out.println(Stream.of(1, 2, 3, 4, 5).reduce(0, new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer integer, Integer integer2) {
                return integer + integer2;
            }
        }));
    }


}
